package com.mes.schedule.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import com.mes.common.spring.SpringUtils;
import com.mes.schedule.domain.ScheduleScheme;
import com.mes.schedule.manager.ScheduleSchemeManager;
/*import com.mes.schedule.ScheduleSchemeManager;
import com.mes.schedule.persistence.domain.ScheduleScheme;
import com.mes.domain.schedule.ScheduleScheme;
import com.mes.manager.schedule.ScheduleSchemeManager;
*/
/**
 * 调度常用方法 
 * @author Administrator  
 * @version   
 * @since JDK 1.6
 */
public class ScheduleUtils {
	// public static SessionScheme getSessionScheme()
	// {
	// return (SessionScheme)SpringUtils.getBean("SessionScheme");
	// }
	static Map<String, ExecutorService> executorServiceMap = new HashMap<String, ExecutorService>();

	public static final int MillSecondsOfDay=86400000;
	public static final int MillSecondsOfMinute=60000;
	//lqm todo 
	//不知道什么原因，在这里会造成有些界面报 ScheduleUtils 无法初始化的错误
//	static ScheduleSchemeManager scheduleSchemeManager = (ScheduleSchemeManager) SpringUtils
//	.getBean("schemeManager");
	/**
	 * 调度运算服务缓冲池
	 */
	public static ExecutorService getExecutorService(ScheduleScheme scheme) {
		ExecutorService executorService = executorServiceMap.get(scheme.getScheduleUid());
//		if (executorService==null || executorService.isShutdown()  )
//		{
//	
//				//用cashedable可能会导致线程膨胀
//				//executorService = Executors.newCachedThreadPool();
//				//20140424
//				//如果是固定的线程池数量，又会导致设备数量超过10时可能会导致设备无限等待的情况
//				executorService=Executors.newFixedThreadPool(10);
//				executorServiceMap.put(scheme.getScheduleUid(), executorService);
//			
//		}

		
		return executorService;
	}

	//允许强制设置执行服务
	public static void setExecutorService(ScheduleScheme scheme,ExecutorService executorService) {
		executorServiceMap.put(scheme.getScheduleUid(), executorService);
	}	
	
	//允许强制设置执行服务
	/**
	 * 强制关闭线程服务
	 */
	public static void closeExecutorService(ScheduleScheme scheme) {
		try {
			ExecutorService executorService = executorServiceMap.get(scheme.getScheduleUid());
			if(executorService==null)return;
			executorServiceMap.remove(scheme.getScheduleUid());
			executorService.shutdownNow();
			executorService = null;
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}	
	
//	/**
//	 * 调度运算服务缓冲池
//	 */
//	public static ExecutorService getCacheableExecutorService(ScheduleScheme scheme) {
//		ExecutorService executorService=null;
//		if (executorServiceMap.containsKey(scheme))
//		{
//			executorService = executorServiceMap.get(scheme);
//		//如果已经关闭
//			if(executorService.isShutdown())
//			{
//				//用cashedable可能会导致线程膨胀
//				executorService = Executors.newCachedThreadPool();
//				//20140424
//				//如果是固定的线程池数量，又会导致设备数量超过10时可能会导致设备无限等待的情况
//				//executorService=Executors.newFixedThreadPool(10);
//				
//				executorServiceMap.put(scheme.getScheduleUid(), executorService);
//			}
//		}
//		else {
//			executorService = Executors.newCachedThreadPool();
//			//如果是固定的线程池数量，又会导致设备数量超过10时可能会导致设备无限等待的情况
//			//executorService=Executors.newFixedThreadPool(10);
//			executorServiceMap.put(scheme.getScheduleUid(), executorService);
//
//		}
//		
//		return executorService;
//	}
	
	public static ScheduleScheme getScheduleScheme(String scheduleUid)
			{
		// 采用spring单例模式定义的schememanager
		ScheduleScheme scheme=null;
		try {
			 ScheduleSchemeManager scheduleSchemeManager = (ScheduleSchemeManager) SpringUtils
			.getBean("schemeManager");
			scheme = scheduleSchemeManager.getScheduleScheme(scheduleUid);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return scheme;
	}

	public static ScheduleScheme getScheduleSchemeByDeptUid(String deptUid)
			throws Exception {
		// 采用spring单例模式定义的schememanager

		String scheduleUid = "schedule-" + deptUid;
		 ScheduleSchemeManager scheduleSchemeManager = (ScheduleSchemeManager) SpringUtils
		.getBean("schemeManager");

		return scheduleSchemeManager.getScheduleScheme(scheduleUid);
	}

	public static String getScheduleUid(String deptUid) throws Exception {
		// 采用spring单例模式定义的schememanager

		String scheduleUid = "schedule-" + deptUid;
		return scheduleUid;
	}

	public static  <T> T  deepClone(Object o) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			ObjectOutputStream oos = new ObjectOutputStream(bos);
			oos.writeObject(o);
			oos.close();

			ByteArrayInputStream bis = new ByteArrayInputStream(bos
					.toByteArray());
			ObjectInputStream ois = new ObjectInputStream(bis);
			Object object = ois.readObject();
			ois.close();
			return (T)object;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}
